-- KeCodeMgr.lua
require("core.Store")

local sformat = string.format

KeCodeMgr = singleton()
local prop = property(KeCodeMgr)
prop:accessor("listeners", {})

function KeCodeMgr:__init()
end

function KeCodeMgr:addListener(listener, event, main_key, sub_key)
    print(sformat("[KeCodeMgr][addListener] event:%s main_key:%s sub_key:%s", event, main_key, sub_key))
    if not self.listeners[event] then
        self.listeners[event] = {}
    end
    self.listeners[event][listener] = {
        main_key = main_key,
        sub_key = sub_key
    }
end

function KeCodeMgr:RemoveListener(listener, event)
    print("[KeCodeMgr][RemoveListener] event:" .. event)
    if self.listeners[event] then
        self.listeners[event][listener] = nil
    end
end

function KeCodeMgr:update()
    for event, items in pairs(self.listeners) do
        for listener, info in pairs(items) do
            if info.main_key and info.sub_key then
                if Input.GetKey(info.main_key) and Input.GetKeyDown(info.sub_key) then
                    listener[event](listener);
                end
            end
        end
    end
end

g_keycode_mgr = KeCodeMgr()
